#!/bin/bash
# 自动化部署脚本
# 作者：胡松
# 版本：v1.0.0
# 邮箱：hoofungson@163.com

# 全局数组
imagePrefix="harbor.hoofungson.cn/hoofungson-blog"
tagName="$1"
appArrays=""

echo "tagName: $tagName"

# 初始化应用
function initApp() {
    for line in `cat applist.txt`
    do
      appArrays[$c]=$line
      ((c++))
    done
}

# 删除本地docker镜像库中镜像
function deleteAppImage() {
    initApp
    for app in ${appArrays[*]}
    do
      echo "正在删除$app镜像文件..."
      docker rmi -f $imagePrefix/$app:$tagName
      echo "==================== 镜像分隔线 ==================="
    done
    echo "已完成删除镜像！！"
}

# 推送docker镜像到远程私有仓库
function pushAppImage() {
    initApp
    for app in ${appArrays[*]}
    do
      echo "正在推送$app镜像文件..."
      docker push $imagePrefix/$app:$tagName
      echo "==================== 镜像分隔线 ==================="
    done
    echo "已完成推送镜像！！"
}

# 创建工作负载
function createAppWorkload() {
    # initApp
    for app in ${appArrays[*]}
    do
      echo "创建$app工作负载！！"
      echo "==================== 负载分隔线=============="
    done
    echo "正在生成id文件..."
    echo "1" > /root/myid
    echo "已生成id文件"
    echo "已完成创建工作负载！！"
}

# 更新工作负载
function updateAppWorkload() {
    # initApp
    for app in ${appArrays[*]}
    do
      echo "更新$app工作负载！！"
      echo "==================== 负载分隔线=============="
    done
    echo "已完成更新工作负载！！"
}

# 部署应用
function deployApp() {
    ##
    pushAppImage
    idFile="/root/myid"
    if [  ! -f $idFile ]; then
      echo "文件不存在！！"
      createAppWorkload
    else
      echo "文件已存在！！"
    fi
}

# 入口函数调用
deployApp